package com.css.zfzx.sjcj.modules.hddcwyVolcanicSamplePoint.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.css.zfzx.sjcj.modules.hddcwyVolcanicSamplePoint.repository.HddcWyVolcanicsamplepointNativeRepository;
import com.css.zfzx.sjcj.modules.hddcwyVolcanicSamplePoint.repository.HddcWyVolcanicsamplepointRepository;
import com.css.zfzx.sjcj.modules.hddcwyVolcanicSamplePoint.repository.entity.HddcWyVolcanicsamplepointEntity;
import com.css.zfzx.sjcj.modules.hddcwyVolcanicSamplePoint.service.HddcWyVolcanicsamplepointService;
import com.css.zfzx.sjcj.modules.hddcwyVolcanicSamplePoint.viewobjects.HddcWyVolcanicsamplepointQueryParams;
import com.css.zfzx.sjcj.modules.hddcwyVolcanicSamplePoint.viewobjects.HddcWyVolcanicsamplepointVO;
import com.css.bpm.platform.api.local.PlatformAPI;
import com.css.bpm.platform.sys.dict.repository.entity.DictItemEntity;
import com.css.bpm.platform.utils.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author zhangcong
 * @date 2020-12-02
 */
@Service
public class HddcWyVolcanicsamplepointServiceImpl implements HddcWyVolcanicsamplepointService {

	@Autowired
    private HddcWyVolcanicsamplepointRepository hddcWyVolcanicsamplepointRepository;
    @Autowired
    private HddcWyVolcanicsamplepointNativeRepository hddcWyVolcanicsamplepointNativeRepository;

    @Override
    public JSONObject queryHddcWyVolcanicsamplepoints(HddcWyVolcanicsamplepointQueryParams queryParams , int curPage,
                                                      int pageSize) {
        Page<HddcWyVolcanicsamplepointEntity> hddcWyVolcanicsamplepointPage =
                this.hddcWyVolcanicsamplepointNativeRepository.queryHddcWyVolcanicsamplepoints(queryParams , curPage, pageSize);
        JSONObject jsonObject = PlatformPageUtils.formatPageData(hddcWyVolcanicsamplepointPage);
        return jsonObject;
    }


    @Override
    public HddcWyVolcanicsamplepointEntity getHddcWyVolcanicsamplepoint(String id) {
        HddcWyVolcanicsamplepointEntity hddcWyVolcanicsamplepoint = this.hddcWyVolcanicsamplepointRepository.findById(id).orElse(null);
         return hddcWyVolcanicsamplepoint;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HddcWyVolcanicsamplepointEntity saveHddcWyVolcanicsamplepoint(HddcWyVolcanicsamplepointEntity hddcWyVolcanicsamplepoint) {
        String uuid = UUIDGenerator.getUUID();
        hddcWyVolcanicsamplepoint.setId(uuid);
        hddcWyVolcanicsamplepoint.setCreateUser(PlatformSessionUtils.getUserId());
        hddcWyVolcanicsamplepoint.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
        this.hddcWyVolcanicsamplepointRepository.save(hddcWyVolcanicsamplepoint);
        return hddcWyVolcanicsamplepoint;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HddcWyVolcanicsamplepointEntity updateHddcWyVolcanicsamplepoint(HddcWyVolcanicsamplepointEntity hddcWyVolcanicsamplepoint) {
        hddcWyVolcanicsamplepoint.setUpdateUser(PlatformSessionUtils.getUserId());
        hddcWyVolcanicsamplepoint.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
        this.hddcWyVolcanicsamplepointRepository.save(hddcWyVolcanicsamplepoint);
        return hddcWyVolcanicsamplepoint;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteHddcWyVolcanicsamplepoints(List<String> ids) {
        List<HddcWyVolcanicsamplepointEntity> hddcWyVolcanicsamplepointList = this.hddcWyVolcanicsamplepointRepository.findAllById(ids);
        if(!PlatformObjectUtils.isEmpty(hddcWyVolcanicsamplepointList) && hddcWyVolcanicsamplepointList.size() > 0) {
            for(HddcWyVolcanicsamplepointEntity hddcWyVolcanicsamplepoint : hddcWyVolcanicsamplepointList) {
                this.hddcWyVolcanicsamplepointRepository.delete(hddcWyVolcanicsamplepoint);
            }
        }
    }

    @Override
    public List<DictItemEntity> getValidDictItemsByDictCode(String dictCode) {
        List<DictItemEntity> validDictItems = PlatformAPI.getDictAPI().getValidDictItemsByDictCode(dictCode);
        return validDictItems;
    }

}
